package com.app.common.ex

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar

inline var View.enable
    set(value) {
        if (isEnabled == value) {
            return
        }
        isEnabled = value
        alpha = if (value) 1f else 0.5f
    }
    get() = isEnabled


fun Context.toast(msg: String) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}

fun View.snackBar(msg: String, actionText: String, block: () -> Unit) {
    Snackbar.make(this, msg, Snackbar.LENGTH_SHORT)
        .setAction(actionText) {
            block()
        }.show()
}

fun Context.toast(msg: Int) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}

fun Fragment.toast(msg: String) {
    Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
}

fun Fragment.toast(msg: Int, gravity: Int = -1) {
    val toast = Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT)
    if (gravity != -1) {
        toast.setGravity(gravity, 0, 0)
    }
    toast.show()
}

fun Activity.toast(msg: Int, gravity: Int = -1) {
    val toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT)
    if (gravity != -1) {
        toast.setGravity(gravity, 0, 0)
    }
    toast.show()
}

fun Activity.statusBar() {
    window.decorView.systemUiVisibility =
        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    window.navigationBarColor = Color.TRANSPARENT
    window.statusBarColor = Color.TRANSPARENT
}

@SuppressLint("ClickableViewAccessibility")
fun View.setPressEffect() {
    setOnTouchListener { v, event ->
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                v.alpha = 0.5f
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                v.alpha = 1f
            }
        }
        false
    }
}